<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>

<body>
	<script>
		var currying = function (fn) {
			var args = Array.prototype.slice.call(arguments, 1);

			return function () {
				if (arguments.length === 0) {
					return fn.apply(this, args); // 没传参数时，调用这个函数
				} else {
					[].push.apply(args, arguments); // 传入了参数，把参数保存下来
					return arguments.callee; // 返回这个函数的引用
				}
			}
		}

		function uncurrying(fn) {
			return function (...args) {
				var ret = fn;

				for (let i = 0; i < args.length; i++) {
					ret = ret(args[i]); // 反复调用currying版本的函数
				}

				return ret; // 返回结果
			};
		}

		var cost = (function () {
			var money = 0;
			return function () {
				for (var i = 0; i < arguments.length; i++) {
					money += arguments[i];
				}
				return money;
			}
		})();

		var curryingCost = currying(cost);
		var uncurryingCost = uncurrying(curryingCost);
		console.log(uncurryingCost(100, 200, 300)()); // 600
	</script>
</body>

</html>